/**
 * 白名单管理MobX Store
 */
import { observable, action } from '../../../miniprogram_npm/mobx-miniprogram/index';
import cloudHelper from '../../../helpers/cloudHelper.js';
import pageHelper from '../../../helpers/pageHelper.js';
import PublicBiz from '../../../comm/biz/public_biz.js';
import AdminWhiteBiz from '../helpers/admin_white_biz.js';
import WhiteBiz from '../helpers/white_biz.js';
import projectSetting from '../../../config/projects_config.js';

export const whiteStore = observable({
  // 白名单列表数据
  dataList: {
    list: [],
    total: 0
  },

  // 列表页查询条件
  search: '',
  sortItems: [],
  sortMenus: [],
  cateIdOptions: [],

  // 表单数据
  formData: AdminWhiteBiz.initFormData(),

  // 加载状态
  isLoad: false,
  isLoading: false,

  // 初始化列表页数据
  initListData: action(function() {
    const cateIdOptions = WhiteBiz.getCateList();

    let sortItems2 = [{ label: '部门', type: '', value: '' }];
    let deptOptions = projectSetting.DEPT_OPTIONS;
    for (let k = 0; k < deptOptions.length; k++) {
      sortItems2.push({ label: deptOptions[k], type: 'dept', value: deptOptions[k] });
    }

    let sortMenus = [
      { label: '全部', type: '', value: '' },
      { label: '正常', type: 'status', value: 1 },
      { label: '停用', type: 'status', value: 0 }, 
    ];

    this.search = '';
    this.cateIdOptions = cateIdOptions;
    this.sortItems = [sortItems2];
    this.sortMenus = [];
    this.isLoad = true;
  }),

  // 加载白名单列表数据
  loadList: action(async function(params) {
    try {
      this.isLoading = true;
      const result = await cloudHelper.dataList('admin/white_list', params);
      this.dataList = result;
      this.isLoading = false;
      return result;
    } catch (e) {
      this.isLoading = false;
      console.error('白名单列表加载失败', e);
    }
  }),

  // 删除白名单项
  delItem: action(async function(id) {
    try {
      let params = { id };
      await cloudHelper.callCloudSumbit('admin/white_del', params, { title: '删除中' });
      
      // 更新本地数据
      const index = this.dataList.list.findIndex(item => item._id === id);
      if (index > -1) {
        this.dataList.list.splice(index, 1);
        this.dataList.total--;
      }
      
      // 清除缓存
      PublicBiz.removeCacheList('admin-white-list');
      PublicBiz.removeCacheList('white-list');
      
      return true;
    } catch (e) {
      console.error('删除失败', e);
      return false;
    }
  }),

  // 初始化表单数据
  initFormData: action(function() {
    this.formData = AdminWhiteBiz.initFormData();
  }),

  // 更新表单字段
  updateForm: action(function(field, value) {
    this.formData[field] = value;
  }),

  // 提交表单数据
  submitForm: action(async function(formComp) {
    try {
      // 获取表单组件的数据
      let forms = formComp.getForms(true);
      if (!forms) return false;
      
      // 整合表单数据
      let data = {...this.formData};
      data.forms = forms;
      data.cateName = AdminWhiteBiz.getCateName(data.formCateId);
      
      // 提交到云函数
      await cloudHelper.callCloudSumbit('admin/white_insert', data);
      
      // 清除缓存
      PublicBiz.removeCacheList('admin-white-list');
      PublicBiz.removeCacheList('white-list');
      
      return true;
    } catch (e) {
      console.error('提交失败', e);
      return false;
    }
  }),

  // 导入白名单数据
  importWhite: action(async function(cloudId) {
    try {
      let params = { cloudId };
      let options = { title: '导入中' };
      
      const result = await cloudHelper.callCloudSumbit('admin/white_import', params, options);
      
      // 清除缓存
      PublicBiz.removeCacheList('admin-white-list');
      PublicBiz.removeCacheList('white-list');
      
      return result.data;
    } catch (e) {
      console.error('导入失败', e);
      return null;
    }
  })
}); 